<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>1_引入生命周期</title>
    <style>
        .input_label {
            margin-left: 10px;
        }
    </style>
</head>
<body>
<!--准备好一个容器-->
<div id="test">

</div>
</body>

<!--核心库引入顺序，必须先是核心库然后在引入dom库-->
<script src="../js/react.development.js" crossorigin></script>
<!--用于支持react操作dom-->
<script src="../js/react-dom.development.js"></script>
<!--引入babel，用于将jsx转为js-->
<script src="../js/babel.min.js"></script>

<script type="text/babel">
    //创建组件
    //生命周期回调函数 = 生命周期钩子函数
    class Life extends React.Component {

        state = {
            opacity: 1
        }

        death = () => {
            //卸载组件
            ReactDOM.unmountComponentAtNode(document.getElementById('test'))
        }

        //组件挂载完成
        componentDidMount() {
            this.timer = setInterval(() => {
                //获取原状态
                let {opacity} = this.state;
                //较少0.1
                opacity -= 0.1
                if (opacity <= 0) {
                    opacity = 1
                }
                this.setState({opacity})
            }, 200)
        }

        //组件将要卸载
        componentWillUnmount(){
            //清除定时器
            clearInterval(this.timer)
        }

        //组件
        render() {
            return (
                <div>
                    <h2 style={{opacity: this.state.opacity}}>React学不会，怎么办？</h2>
                    <button onClick={this.death}>不活了</button>
                </div>
            )
        }
    }

    //2·渲染组件
    ReactDOM.render(<Life/>, document.getElementById('test'))
</script>
</html>